package cn.highedu.spring.bean13;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class TransferServiceImpl implements TransferService {

    @Autowired
    @Qualifier("jdbcAccountRepository")
    AccountRepository accountRepository;

    public TransferServiceImpl(){
    }

    @Autowired
    public TransferServiceImpl(
            @Qualifier("jdbcAccountRepository") AccountRepository accountRepository){
        this.accountRepository = accountRepository;
    }

    public void transfer(int money){
        System.out.println("转账："+money);
    }

    // @Autowired
    public void a(AccountRepository accountRepository){
        System.out.println("初始化");
        this.accountRepository = accountRepository;
    }

    @Override
    // @Autowired(required = false) // required = false，表示不是必须注入，如果没有找到对应的bean，会忽略
    @Autowired // 默认required = true，表示必须注入，如果没有找到对应的bean，会报错
    public void setAccountRepository(
            @Qualifier("jpaAccountRepository") AccountRepository accountRepository) {
        System.out.println(this.accountRepository);
        this.accountRepository = accountRepository;
    }

    @Override
    public AccountRepository getAccountRepository() {
        return accountRepository;
    }
}
